﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.IService;
using System.Data;
using Utility;
using System.Collections;
using System.Data.SqlClient;
using Entity;


namespace DataAccess.Service
{
    public class UtilityPaymentService : IUtilityPaymentService
    {
        public UtilityPaymentProject selectUtilPaymentThisMonth(Dormitory dormitory,String date)
        {
            UtilityPaymentProject project = null;
           
            string commandText = string.Format("select * from  UtilityPayment where dorId=@dorId and date=@date and status=@status");
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("dorId", dormitory.DormitoryId),
                new SqlParameter("date", date),
                new SqlParameter("status" ,"未缴清")
            };
            SqlDataReader sdReader = SQLHelper.ExecuteReader(commandText, paras);
            if (sdReader.Read())
            {
                project = new UtilityPaymentProject();
                project.WaterMeterNum= Convert.ToDecimal(sdReader["waterUseNum"]);
                project.ElecMeterNum = Convert.ToDecimal(sdReader["elecUseNum"]);
                project.Date = Convert.ToString(sdReader["date"]);
                project.Id = Convert.ToInt32(sdReader["utilityId"]);
                project.WaterBill = Convert.ToDecimal(sdReader["waterBill"]);
                project.ElectricityBill = Convert.ToDecimal(sdReader["elecBill"]);
                
            }

            return project;
        }
        public UtilityPaymentProject selectUtilPaymentLastMonth(Dormitory dormitory, String date)
        {
            UtilityPaymentProject project = new UtilityPaymentProject();
            

            string commandText = string.Format("select * from  UtilityPayment where dorId=@dorId and date=@date ");//and status=@status");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("dorId", dormitory.DormitoryId),
                new SqlParameter("date", date),
                //new SqlParameter("status" ,"已缴费")
            };
            SqlDataReader sdReader = SQLHelper.ExecuteReader(commandText, paras);
            if (sdReader.Read())
            {

                project.WaterMeterNum = Convert.ToDecimal(sdReader["waterUseNum"]);
                project.ElecMeterNum = Convert.ToDecimal(sdReader["elecUseNum"]);
                project.Date = Convert.ToString(sdReader["date"]);
                project.Id = Convert.ToInt32(sdReader["utilityId"]);
            }

            return project;
        }

        public bool updateUtilityInfor(int utilityId)
        {
            bool result = false;
            string commandText = string.Format("update UtilityPayment set status=@status where utilityId=@id");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("status", "已支付"),
                new SqlParameter("id",utilityId)
            };

            if (SQLHelper.ExecuteNonQuery(commandText, paras) > 0)
            {
                result = true;
            }
            return result;
        }

        public bool insertUtilityPayment(List<UtilityPaymentProject> projects)
        {
            bool result = false;
            string commandText = "insert UtilityPayment(waterUseNum,elecUseNum,dorId,status,date) values(@waterUseNum,@elecUseNum,@dorId,@status,@date) ";
            for (int i = 0; i < projects.Count; i++)
            {
                   
                  SqlParameter[] paras = new SqlParameter[]
                { 
                    new SqlParameter("waterUseNum", projects[i].WaterMeterNum),
                    new SqlParameter("elecUseNum", projects[i].ElecMeterNum),
                    new SqlParameter("dorId", projects[i].Dormitory.DormitoryId),
                    new SqlParameter("status", "未付款"),
                    new SqlParameter("date", projects[i].Date)
                };
                if (SQLHelper.ExecuteNonQuery(commandText, paras) > 0)
               {
                      result = true;
                }
            }
         
            return result;

        }
    }
}
